using System;
using System.Web;
using System.Web.UI;

namespace ContinualFlame.Locus
{	
	public class ClientActions : System.Web.IHttpHandler
	{		
		public virtual bool IsReusable
		{
			get
			{
				return false;
			}
		}
		
		public virtual void ProcessRequest(HttpContext context)
		{
			HttpRequest req = context.Request;
            HttpResponse rsp = context.Response;

            if (req.HttpMethod == "POST")
            {
				string sid = req.Params["id"];
				string cid = req.Cookies.Get(ContinualFlame.Locus.Session.SessionCookieName).Value;
				string ev = req.Params["event"];
				string msg = req.Params["msg"];
				
				//
				// auth check
				//
				
				Session s = SessionManager.Instance.GetSessionById(sid);
				
				if (!s.AuthorizedClients.ContainsKey(cid))
				{
					// TODO: the cool kids would return a meaningful error code
					throw new ApplicationException("Unknown user or session. Has your game expired?");
				}
				
				s.Controller.Process(ev, msg);
			}
			else
			{
				// TODO: the cool kids would return a meaningful error code
				throw new ApplicationException("invalid HTTP method: only GET is supported.");
			}
		}
	}
}
